package com.ecoolex.ying.space.api.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.ying.space.entity.DesignerProject;
import com.ecoolex.ying.space.entity.GoodsWithBLOBs;
import com.ecoolex.ying.space.entity.UserGoodsAttachmentDownloadTask;
import com.ecoolex.ying.space.pojo.QueryUserGoodsAttachmentDownloadTaskDTO;
import com.ecoolex.ying.space.ws.DesignerProjectService;
import com.ecoolex.ying.space.ws.GoodsService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import java.util.List;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.*;

@GraphQL2Module(type = Type.User, description = "用户商品附件下载管理")
public class UserGoodsAttachmentDownloadTaskGraphql {

    @Reference
    private DesignerProjectService designerProjectService;
    @Reference
    private GoodsService goodsService;

    @Autowired
    private StorageService storageService;

    @TypeField(type = UserGoodsAttachmentDownloadTask.class, field = "downloadZipStorage", description = "下载zip资源")
    public StorageInfo downloadZipStorage(@Source UserGoodsAttachmentDownloadTask source) {
        return storageService.get(source.getDownloadStorageId());
    }

    @MutationField(field = "addUserGoodsAttachmentDownloadTaskByGoods", description = "根据商品添加用户商品附件下载任务")
    public UserGoodsAttachmentDownloadTask addPickGoodsFavorite(@NonNull Integer goodsId, @NonNull Integer goodsAttachmentType, ConsumerMemberAuthority user) {
        user.assertLogin();
        GoodsWithBLOBs goods = goodsService.getGoodsById(goodsId);
        Assert.isTrue(Check.notNull(goods), ResultCode.VERIFY.build(1), "商品不存在");
        return designerProjectService.createDownloadTaskByGoods(user.userId(), goods, goodsAttachmentType, user.userName());
    }

    @MutationField(field = "addUserGoodsAttachmentDownloadTaskByProject", description = "根据项目添加用户商品附件下载任务")
    public UserGoodsAttachmentDownloadTask addUserGoodsAttachmentDownloadTaskByProject(@NonNull Integer projectId, @NonNull Integer goodsAttachmentType, ConsumerMemberAuthority user) {
        user.assertLogin();
        DesignerProject project = designerProjectService.getProjectByUserIdAndId(user.userId(), projectId);
        Assert.isTrue(Check.notNull(project), ResultCode.VERIFY.build(1), "项目不存在");
        return designerProjectService.createDownloadTaskByProject(user.userId(), project, goodsAttachmentType, user.userName());
    }

    @MutationField(field = "delUserGoodsAttachmentDownloadTask", description = "删除用户商品附件下载任务")
    public boolean delUserGoodsAttachmentDownloadTask(@NonNull Integer taskId, ConsumerMemberAuthority user) {
        user.assertLogin();
        designerProjectService.deleteDownloadTask(user.userId(), taskId, user.userName());
        return true;
    }

    @QueryField(field = "userGoodsAttachmentDownloadTaskList", description = "用户商品附件下载任务列表")
    public Pagination<UserGoodsAttachmentDownloadTask> query(@NonNull QueryUserGoodsAttachmentDownloadTaskDTO query, @NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return designerProjectService.queryDownloadTaskByUserId(user.userId(), query, sorts, page);
    }

    @QueryField(field = "userGoodsAttachmentDownloadTaskListByMaxId", description = "用户商品附件下载任务列表")
    public List<UserGoodsAttachmentDownloadTask> query(Integer maxTaskId, @NonNull Integer size, ConsumerMemberAuthority user) {
        user.assertLogin();
        return designerProjectService.queryDownloadTaskByUserId(user.userId(), maxTaskId, size);
    }


}
